package com.mh.service;

import java.util.List;

import com.mh.entity.Book;
import com.mh.entity.BookType;
import com.mh.jdbc.entity.IPage;
import com.mh.jdbc.entity.MBean;
import com.mh.jdbc.exception.DAOException;
import com.mh.jdbc.util.BeanUtil;
import com.mh.jdbc.util.JdbcDao;

public class BookService {
	/**
	 * 遍历图书类型
	 * @return
	 */
	public List<BookType> findBookType(){
		try {
			List<MBean> mBeanList = JdbcDao.queryForListEntity(new BookType());
			return BeanUtil.toTargetEntity(mBeanList, BookType.class);
		} catch (DAOException e) {
			throw new RuntimeException(e);
		}
	}
	/**
	 * 书名校验
	 * @param book
	 * @return
	 */
	public boolean bookIfInsert(String bookName){
		try {
			Book book = new Book();
			book.setBookName(bookName);
			return JdbcDao.ifCanInsert(book, "bookName");
		} catch (DAOException e) {
			throw new RuntimeException(e);
		}
	}
	/**
	 * 添加图书
	 * @param book
	 * @return
	 */
	public int addBook(Book book){
		try {
			return JdbcDao.insert(book);
		} catch (DAOException e) {
			throw new RuntimeException(e);
		}
	}
	/**
	 * 删除图书
	 * @param book
	 * @return
	 */
	public int deleteBook(Book book){
		try {
			return JdbcDao.delete(book);
		} catch (DAOException e) {
			throw new RuntimeException(e);
		}
	}
	/**
	 * 修改图书
	 * @param book
	 * @return
	 */
	public int modifyBook(Book book){
		try {
			return JdbcDao.update(book, "id");
		} catch (DAOException e) {
			throw new RuntimeException(e);
		}
	}
	/**
	 * 查询图书
	 * @param iPage
	 * @return
	 */
	public IPage<Book> findBook(IPage<Book> iPage){
		try {
			iPage.setLikePro("bookName");
			return JdbcDao.queryForPageLeftLikeWhere(iPage);
		} catch (DAOException e) {
			throw new RuntimeException(e);
		}
	}
	/**
	 * 查询图书类型
	 * @param iPage
	 * @return
	 */
	public Object findBookById(Integer bookTypeId){
		try {
			BookType bookType = new BookType();
			bookType.setId(bookTypeId);
			return JdbcDao.queryForObject(bookType, "bookTypeName");
		} catch (DAOException e) {
			throw new RuntimeException(e);
		}
	}
}
